/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 *
 */


import { expect } from '@jest/globals';
import fse from 'fs-extra';

import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
  modulesOption: [
    ModuleTemplateType.StaticLibrary,
  ]
};

/**
 * 验证如果自定义目录不存在是否可以正确编译
 *
 * @param project
 */
const testfun: TestCaseFun = async (project) => {
  const projectRoot = project.projectRootPath();
  fse.removeSync('D:/e2e_test_build_cache');
  fse.removeSync('D:/e2e_hvigor_build_cache');

  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: ['assembleHar'],
    buildCacheDir: 'D:/e2e_test_build_cache',
    hvigorCacheDir: 'D:/e2e_hvigor_build_cache',
  });
  const result = await runHvigorAsync(projectRoot, commands);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
  fse.removeSync('D:/e2e_test_build_cache');
  fse.removeSync('D:/e2e_hvigor_build_cache');
};

TestCaseBuilder.builder()
  .caseName('custom_build_cache')
  // eslint-disable-next-line max-len
  .description('自定义构建缓存路径不存在时编译成功')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();
